package com.example.testlxr9096.itemActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.provider.Settings;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.testlxr9096.R;

import java.text.DecimalFormat;

public class SettingsActivity extends Activity {

    private TextView storageInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        initViews();
        setupClickListeners();
        updateStorageInfo();
    }

    private void initViews() {
        storageInfo = findViewById(R.id.storage_info);
    }

    private void setupClickListeners() {
        // WiFi设置
        LinearLayout wifiSettings = findViewById(R.id.wifi_settings);
        wifiSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
                    startActivity(intent);
                } catch (Exception e) {
                    showToast("无法打开WiFi设置");
                }
            }
        });

        // 显示设置
        LinearLayout displaySettings = findViewById(R.id.display_settings);
        displaySettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent = new Intent(Settings.ACTION_DISPLAY_SETTINGS);
                    startActivity(intent);
                } catch (Exception e) {
                    showToast("无法打开显示设置");
                }
            }
        });

        // 声音设置
        LinearLayout soundSettings = findViewById(R.id.sound_settings);
        soundSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent = new Intent(Settings.ACTION_SOUND_SETTINGS);
                    startActivity(intent);
                } catch (Exception e) {
                    showToast("无法打开声音设置");
                }
            }
        });

        // 存储设置
        LinearLayout storageSettings = findViewById(R.id.storage_settings);
        storageSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent = new Intent(Settings.ACTION_INTERNAL_STORAGE_SETTINGS);
                    startActivity(intent);
                } catch (Exception e) {
                    showToast("无法打开存储设置");
                }
            }
        });

        // 日期时间设置
        LinearLayout dateTimeSettings = findViewById(R.id.date_time_settings);
        dateTimeSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent = new Intent(Settings.ACTION_DATE_SETTINGS);
                    startActivity(intent);
                } catch (Exception e) {
                    showToast("无法打开日期时间设置");
                }
            }
        });
        // 日期时间设置
        LinearLayout recordSetting = findViewById(R.id.record_settings);
        recordSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Intent intent = new Intent(SettingsActivity.this, TestRecordActivity.class);
                    startActivity(intent);
                } catch (Exception e) {
                    showToast("无法打开日期时间设置");
                }
            }
        });
    }

    private void updateStorageInfo() {
        try {
            StatFs statFs = new StatFs(Environment.getDataDirectory().getPath());
            long blockSize = statFs.getBlockSizeLong();
            long totalBlocks = statFs.getBlockCountLong();
            long availableBlocks = statFs.getAvailableBlocksLong();

            long totalSize = totalBlocks * blockSize;
            long availableSize = availableBlocks * blockSize;
            long usedSize = totalSize - availableSize;

            double usedGB = (double) usedSize / (1024 * 1024 * 1024);
            double totalGB = (double) totalSize / (1024 * 1024 * 1024);

            DecimalFormat df = new DecimalFormat("#.##");
            String info = "已使用 " + df.format(usedGB) + " GB / 总共 " + df.format(totalGB) + " GB";

            storageInfo.setText(info);
        } catch (Exception e) {
            storageInfo.setText("存储信息不可用");
        }
    }

    private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 当从系统设置返回时更新存储信息
        updateStorageInfo();
    }
}